package cn.haoxsf.app.system.controller;

import cn.haoxsf.app.common.api.CommonResult;
import cn.haoxsf.app.common.api.enums.SystemExceptionEnum;
import cn.haoxsf.app.common.util.ToolUtil;
import cn.haoxsf.app.system.entity.MemberRole;
import cn.haoxsf.app.system.service.IBannerService;
import cn.haoxsf.app.system.service.IMemberRoleService;
import cn.haoxsf.app.system.service.IMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 首页数据 前端控制器
 * </p>
 *
 * @author Ele  qvbanfm@qq.com
 * @since 2020-01-06
 */
@RestController
@RequestMapping("/index")
@Api(tags = "首页数据", description = "用于首页数据的相关请求")
public class IndexController {
    @Autowired
    private IMemberService memberService;
    @Value("${radii}")
    private Double radii;//附近的范围

    @Autowired
    private IMemberRoleService roleService;
    @Autowired
    private IBannerService bannerService;

    //附近的师傅
    @GetMapping("nearby")
    @ApiOperation(value = "展示师傅", notes = "根据经纬度获取附近的师傅")
    public CommonResult nearbyBusiness(@ApiParam(value = "经纬度", required = true, example = "22.819695")
                                       @RequestParam(required = false) Double lat,
                                       @ApiParam(value = "经纬度", required = true, example = "108.328172")
                                       @RequestParam(required = false) Double lng,
                                       @ApiParam(value = "师傅类型-目前有: TRAM=电车维修，APPLIANCE=家电 ,AIR_CONDITIONER=空调，CAR=汽车", required = true, example = "TRAM")
                                       @RequestParam(required = false) String userType) {
        return CommonResult.success(memberService.getNearbyUsers(radii, lng, lat, userType));
    }

    @GetMapping("nav")
    @ApiOperation(value = "导航栏", notes = "用户首页的师傅类型的导航栏")
    public CommonResult getNavBar() {
        List<MemberRole> roleAll = roleService.findRoleAll();
        if (ToolUtil.isEmpty(roleAll)) {
            return CommonResult.error(SystemExceptionEnum.VALIDATE_FAILED);
        }
        return CommonResult.success(roleAll);
    }
    @GetMapping("banner")
    @ApiOperation(value = "首页幻灯片", notes = "首页幻灯片 = INDEX_BANNER 为首页的")
    public CommonResult getBanner(@RequestParam(defaultValue = "INDEX_BANNER") String banner){
        return CommonResult.success(bannerService.getList(banner));
    }
}
